package com.css.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.css.service.ICarService;
import com.css.service.ICustomerService;
import com.css.service.IEmployeeService;
import com.css.service.ISaleService;
import com.css.vo.Sale;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class SaleAction extends ActionSupport {

	private Sale sale;
	private List<Sale> saleList;
	private ISaleService saleService;
	private ICarService carService;
	private ICustomerService customerService;
	private IEmployeeService employeeService;
	private Integer saleId;

	private int page = 1;
	private int pageSize = 10;
	private String search;
	private Map<String, Object> result;

	public String execute() throws Exception {
		return SUCCESS;
	}

	public Sale getSale() {
		return sale;
	}

	public void setSale(Sale sale) {
		this.sale = sale;
	}

	public ISaleService getSaleService() {
		return saleService;
	}

	public void setSaleService(ISaleService saleService) {
		this.saleService = saleService;
	}

	public String addSale() {
		try {
			saleService.save(sale);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	public String findAllSale() {
		try {
			saleList = saleService.findAllSale();
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	public String deleteSale() {
		try {
			HttpServletRequest request = (HttpServletRequest) ActionContext
					.getContext().get(ServletActionContext.HTTP_REQUEST);
			String saleIdStr = request.getParameter("saleId");
			Integer saleId = Integer.parseInt(saleIdStr);
			Sale sale = saleService.findById(saleId);

			saleService.delete(sale);
			return SUCCESS;

		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	public String updateSaleView() {
		try {
			HttpServletRequest request = (HttpServletRequest) ActionContext
					.getContext().get(ServletActionContext.HTTP_REQUEST);
			String saleIdStr = request.getParameter("saleId");
			Integer saleId = Integer.parseInt(saleIdStr);
			sale = saleService.findById(saleId);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	public String updateSale() {
		try {
			saleService.update(sale);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	public String searchSale() {
		try {
			saleList = saleService.searchSale(search);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	public String salePage() {
		try {
			List<Sale> saleList = saleService.findAllSale();
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	public String saleTop() {
		return SUCCESS;
	}

}
